<?php
require_once('db.php');
class Course{
  //
  public function showAllCrouse(){
    $conn=getConn();
    $sql="select distinct courseName from course";
    $result = mysqli_query($conn,$sql);
    $resArr=array();
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
      array_push($resArr,$row['courseName']);
    }
    mysqli_close($conn);
    return $resArr;    
  }
  public function showAllStudentByCourse($courseName){
    $conn=getConn();
    $sql="select * from course where courseName='".$courseName."'";
    $result = mysqli_query($conn,$sql);
    $resArr=array();
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
      $temp='{"id":'.$row['id'].',"studentId":'.$row['studentId'].',"courseScore":'.$row['courseScore'].',"weight":'.$row['weight'].'}';
      array_push($resArr,$temp);
    }
    mysqli_close($conn);
    return $resArr;    
  }
  public function showAllInfoByStudent($studentId){
    $conn=getConn();
    $sql="select * from course where studentId=".$studentId;
    $result = mysqli_query($conn,$sql);
    $resArr=array();
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
      $temp='{"id":'.$row['id'].',"weight":'.$row['weight'].',"courseName":"'.$row['courseName'].'","courseScore":'.$row['courseScore'].',"kind":"'.$row['kind'].'"}';
      array_push($resArr,$temp);
    }
    mysqli_close($conn);
    return $resArr;    
  }
  public function insert($studentId,$courseName,$courseScore,$weight,$kind){
    $conn=getConn();
    $sql="insert into course (studentId,courseName,courseScore,weight,kind) values ('".$studentId."','".$courseName."','".$courseScore."',".$weight.",'".$kind."')";
    $result=mysqli_query($conn,$sql);
    mysqli_close($conn);
    return $result;
  }
  public function update($id,$courseScore,$weight){
    $conn=getConn();
    $sql="update course set courseScore=".$courseScore.",weight = ".$weight." where id=".$id;
    $result=mysqli_query($conn,$sql);
    mysqli_close($conn);
    return $result;
  }
  public function del($id){
    $conn=getConn();
    $sql="delete from course where id=".$id;
    $result=mysqli_query($conn,$sql);
    mysqli_close($conn);
    return $result;
  }
}
?>